CXX=g++
CXXFLAGS=-Wall -Wextra -Winit-self -Wstrict-aliasing=2 -Wno-comment -Wno-missing-field-initializers -Wno-unused -fPIC -DPTRACING=1 -D_REENTRANT -D_GNU_SOURCE=1 -felide-constructors -Wreorder -I../common/include -Iinclude -I../protocols/include
LDFLAGS=-lfftw3 -lgsl -lgslcblas -lpthread
LIBS=-L../common/build-$(ARCH) -lcommon -lcryptopp
CXXFLAGS_ARCH=
LDFLAGS_ARCH=
ifeq ($(DEBUG_BUILD),yes)
CXXFLAGS += -ggdb -O0 -DDEBUG
endif # DEBUG_BUILD
CXXFLAGS += $(CXXFLAGS_ARCH)
LDFLAGS += $(LDFLAGS_ARCH)
ifndef $(ARCH)
ARCH = default
endif

RM=rm
RMFLAGS=-rf
MKDIR=mkdir
AR=ar
ARFLAGS=rcs

SOURCE_DIR=src
LSB_SOURCE_DIR=$(SOURCE_DIR)/lsb
CRYPTO_SOURCE_DIR=$(SOURCE_DIR)/crypto

BUILD_DIR=build-$(ARCH)
LSB_BUILD_DIR=$(BUILD_DIR)/lsb
CRYPTO_BUILD_DIR=$(BUILD_DIR)/crypto

SOURCE_FILES=$(shell cd $(SOURCE_DIR) ; ls *.cxx)
LSB_SOURCE_FILES=$(shell cd $(LSB_SOURCE_DIR) ; ls *.cxx)
CRYPTO_SOURCE_FILES=$(shell cd $(CRYPTO_SOURCE_DIR) ; ls *.cxx)

LIBOUT=$(BUILD_DIR)/libembedding.a

OBJ=$(SOURCE_FILES:%.cxx=$(BUILD_DIR)/%.o)
LSB_OBJ=$(LSB_SOURCE_FILES:%.cxx=$(LSB_BUILD_DIR)/%.o)
CRYPTO_OBJ=$(CRYPTO_SOURCE_FILES:%.cxx=$(CRYPTO_BUILD_DIR)/%.o)
AESTEST_OBJ=$(CRYPTO_BUILD_DIR)/cipher_stream.o \
			$(CRYPTO_BUILD_DIR)/aes_cipher_stream.o

default: debug

release: all

all: $(LIBOUT)

debug:
	$(MAKE) DEBUG_BUILD=yes all

$(LIBOUT): init $(OBJ) $(LSB_OBJ) $(CRYPTO_OBJ)
	$(AR) $(ARFLAGS) $(LIBOUT) $(OBJ) $(LSB_OBJ) $(CRYPTO_OBJ)

$(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.cxx
	$(CXX) -c $(CXXFLAGS) -o $@ $<

$(LSB_BUILD_DIR)/%.o: $(LSB_SOURCE_DIR)/%.cxx
	$(CXX) -c $(CXXFLAGS) -o $@ $<

$(CRYPTO_BUILD_DIR)/%.o: $(CRYPTO_SOURCE_DIR)/%.cxx
	$(CXX) -c $(CXXFLAGS) -o $@ $<

init:
	@if [ ! -d $(BUILD_DIR) ] ; then $(MKDIR) -p $(BUILD_DIR) ; fi
	@if [ ! -d $(LSB_BUILD_DIR) ] ; then $(MKDIR) -p $(LSB_BUILD_DIR) ; fi
	@if [ ! -d $(CRYPTO_BUILD_DIR) ] ; then $(MKDIR) -p $(CRYPTO_BUILD_DIR) ; fi

.PHONY: clean

clean:
	$(RM) $(RMFLAGS) $(BUILD_DIR) $(LSB_BUILD_DIR) $(CRYPTO_BUILD_DIR)

aestest:
	$(CXX) -c $(CXXFLAGS) -o $(CRYPTO_BUILD_DIR)/cipher_stream.o $(CRYPTO_SOURCE_DIR)/cipher_stream.cxx
	$(CXX) -c $(CXXFLAGS) $(CRYPTO_SOURCE_DIR)/aes_cipher_stream.cxx -o $(CRYPTO_BUILD_DIR)/aes_cipher_stream.o $(LIBS)
	
	$(CXX) $(CXXFLAGS) $(AESTEST_OBJ) $(CRYPTO_SOURCE_DIR)/aestest.cxx -o $(CRYPTO_BUILD_DIR)/aestest $(LIBS)
	